﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class WB18GIData : BaseFunction
    {


        public WB18GIData()
            : base()
        {

        }

        public WB18GIData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        } 
        public DataSet BuildData(DataRow dwThis)
        {
            DataSet setReturn = new DataSet();
            DataSet dsResult = new DataSet();
            string  LOT, ARTICLE;
            LOT = dwThis["LOT"].ToString().Trim();
            ARTICLE = dwThis["ARTICLE"].ToString().Trim();
            try
            {

                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT C.WBBDPART,B.WBFKRWKRS ||B.WBFKRWLFD||B.WBFKRWPOS AS CARDNO, "
                    + " C.WBBSTKNR,B.WBFKVERSION,B.WBFKNEUDAT,D.HRNNAME,B.WBFKSART,C.WBBZENSUR||C.WBBZENSURKZ AS GRADE, "
                    + " TRIM(C.WBBART)||'-'||TRIM(C.WBBFARBE)||'-'||C.WBBVAR AS ARTICLE, "
                    + " ROUND(C.WBBSSTKLG / 1000 / 0.9144) AS YARDQTY,'' AS REMARK "
                    + " FROM WBFKOPF B,WBBST C,HRSTAMM D "
                    + " WHERE B.WBFKMANDANT = C.WBBMANDANT AND B.WBFKRWKRS = C.WBBRWKRS "
                    + " AND B.WBFKRWLFD = C.WBBRWLFD AND B.WBFKRWPOS = C.WBBPOS "
                    + " AND B.WBFKMANDANT = D.HRMANDANT AND B.WBFKWARSCH = D.HRBENID "
                    + " AND B.WBFKAUFLFD = 0 "
                    + " AND (B.WBFKNEUDAT >= :FROMINSPECTIONDATE OR :FROMINSPECTIONDATE IS NULL) "
                    + " AND (B.WBFKNEUDAT <= :ENDINSPECTIONDATE OR :ENDINSPECTIONDATE IS NULL) "
                    + " AND C.WBBDPART  LIKE '" + LOT + "%' AND C.WBBWAKZ = :TYPE  "
                    + " AND TRIM(C.WBBART)||'-'||TRIM(C.WBBFARBE)||'-'||C.WBBVAR LIKE '" + ARTICLE + "%' "
                    ;
                //FROMINSPECTIONDATE ENDINSPECTIONDATE LOT
                orlReadCommand.Parameters.Add("FROMINSPECTIONDATE", OracleType.NVarChar).Value = dwThis["FROMINSPECTIONDATE"];
                orlReadCommand.Parameters.Add("ENDINSPECTIONDATE", OracleType.NVarChar).Value = dwThis["ENDINSPECTIONDATE"];
                orlReadCommand.Parameters.Add("TYPE", OracleType.NVarChar).Value = dwThis["TYPE"];
                //orlReadCommand.Parameters.Add("LOT", OracleType.VarChar).Value = dwThis["LOT"];
                //orlReadCommand.Parameters.Add("ARTICLE", OracleType.VarChar).Value = dwThis["ARTICLE"];
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "PIECE_INSPECTION"; 
                    byte[] b;
                    string value;
                    string ColumnName = "",REMARK;
                    foreach (DataRow dwTemp in dsResult.Tables["PIECE_INSPECTION"].Rows)
                    {
                        ColumnName = "HRNNAME";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[ColumnName] = value;
                        string CARDNO = dwTemp["CARDNO"].ToString() + dwTemp["WBFKVERSION"].ToString();
                        REMARK = GetCommentsFromTXTIND("WBF", CARDNO);
                        dwTemp["REMARK"] = REMARK.Trim();
                    }
                }
                setReturn.Merge(dsResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}